<template>
  <div class="app-container pagination">
    <el-card>
      <div class="header">
        <el-input v-model="search['name']" placeholder="请输入网点名称" class="search-input"/>
        <el-button type="primary" icon="el-icon-search" @click="()=>pagination['load']()">搜索</el-button>
        <router-link to="./add">
          <el-button type="warning" icon="el-icon-printer">添加网点</el-button>
        </router-link>
      </div>
      <ws-pagination v-model="pagination">
        <el-table-column prop="numberId" label="网点ID" min-width="150" header-align="center" align="center"/>
        <el-table-column prop="name" label="网点名称" min-width="150" header-align="center" align="center"/>
        <el-table-column prop="address" label="网点地址" min-width="120" header-align="center" align="center"/>
        <el-table-column prop="mobile" label="联系电话" min-width="120" header-align="center" align="center"/>
        <el-table-column prop="businessHours" label="工作时间" min-width="240" header-align="center" align="center"/>
        <el-table-column prop="score" label="网点评分" width="100" header-align="center" align="center"/>
        <el-table-column label="状态" width="120" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <div @click="changeStatus(scope.row)">
              <el-tag :type="scope.row.status===2?'danger':'success'">{{ scope.row.status===2?'已禁用':'已启用' }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <!--详情-->
            <el-tooltip class="item" effect="dark" content="网点车辆管理" placement="top">
              <router-link :to="`./car?id=${scope['row']['id']}`">
                <el-button size="small" type="primary" icon="el-icon-more" circle/>
              </router-link>
            </el-tooltip>
            <!--编辑-->
            <router-link :to="`./edit?id=${scope['row']['id']}`">
              <el-button size="small" type="info" icon="el-icon-edit" circle/>
            </router-link>
            <!--删除-->
            <el-button size="small" type="danger" icon="el-icon-delete" circle @click="deleteRow(scope['row'])"/>
          </template>
        </el-table-column>
      </ws-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'RentCarBranchList',
    components: {},
    data() {
      return {
        search: {
          name: ''
        },
        pagination: {}
      }
    },
    watch: {},
    created() {
      // 初始化分页器
      this.initPagination();
    },
    methods: {
      // 初始化分页器
      initPagination() {
        // 创建分页器
        this.pagination = new this.$pagination(async (...args) => {
          // 加载分页列表
          const result = await this.$service.applicationGetRentCarBranchList({...args[0], name: this.search.name});
          if (result.code !== 0) {
            return this.$message.error(result.msg || '系统错误');
          }
          return result;
        });
        // 加载分页数据
        this.pagination.load();
      },
      // 删除指定行
      deleteRow({id}) {
        this.$confirm('此操作将删除该网点, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              const result = await this.$service.applicationDeleteRentCarBranch({id: id});
              if (result.code === 0) {
                this.$message.success(`删除成功~`);
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        });
      },
      // 切换上下架状态
      changeStatus({id, status}) {
        this.$confirm(`您确定要${status === 1 ? '禁用' : '启用'}该网点吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              const result = await this.$service.applicationRentCarBranchChangeStatus({
                id: id,
                status: status === 1 ? 2 : 1
              });
              if (result.code === 0) {
                this.$message.success(`${status === 1 ? '禁用' : '启用'}成功~`);
                // 加载分页数据
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        });
      }
    }
  }
</script>
